#include <string>
#include <iostream>
#include <fstream> 
#include <sstream>
#include <stdio.h>

#include "kungfu/practice/hero.h"
#include "kungfu/practice/master.h"
#include "kungfu/practice/apprentice.h"

#include "kungfu/wingchun/service/ledger.h"
#include "kungfu/wingchun/algo/algo.h"
#include "kungfu/wingchun/strategy/runner.h"

#include "kungfu/yijinjing/common.h"
#include "kungfu/yijinjing/time.h"

#include "mint/desk/ctp/trader_ctp.h"
#include "mint/desk/ctp/marketdata_ctp.h"

#include "mint/service/mlocator.h"
#include "mint/service/mledger.h"
#include "mint/startup/startup.h"

#include "nlohmann/json.hpp"

using namespace std;
using namespace kungfu::practice;

using namespace kungfu::wingchun::ctp;
using namespace kungfu::wingchun::algo;
using namespace kungfu::wingchun::service;
using namespace kungfu::wingchun::msg::data;

using namespace kungfu::yijinjing::data;
using namespace kungfu::yijinjing::journal;
using namespace kungfu::yijinjing::nanomsg;

using namespace junyi::mint::wrapper;


namespace junyi
{
	namespace mint
	{
		namespace wrapper
		{
			StartUp::StartUp()
			{
				ms_locator_ptr = std::make_shared<MLocator>();
				ms_locator_ptr.get()->home = "C:\\Users\\DELL\\AppData\\Roaming\\kungfu\\app";
			}

			StartUp::StartUp(const std::string &home)
			{
				ms_locator_ptr = std::make_shared<MLocator>();
				ms_locator_ptr.get()->home = home;
			}

			StartUp::StartUp(const std::string &home, const std::string &config)
			{
				ms_locator_ptr = std::make_shared<MLocator>();
				ms_locator_ptr.get()->home = home;

				config_char = strdup(config.c_str());
				std::cout << "on construct config_char:" << config_char << endl;

				config_string = config;
				std::cout << "on construct config_string:" << config_string << endl;
			}

			StartUp::~StartUp()
			{
			}

			void StartUp::load_master() {
				cout << "call master" << endl;

				location mst_location = location(mode::LIVE, category::SYSTEM, "master", "master", ms_locator_ptr);
				std::shared_ptr<location> mst_location_ptr = make_shared<location>(mst_location);
				cout << "call master mst_location" << endl;
				master mst_progress = master(mst_location_ptr, false);
				cout << "call master mst_location progress" << endl;
				mst_progress.run();
			}

			void StartUp::load_ledger() {
				cout << "call ledger" << endl;

				// ============start ledger thread============
				MLedger mst_ledger = MLedger(ms_locator_ptr, mode::LIVE, false);
				mst_ledger.run();
			}

			void StartUp::load_trader() {
				cout << "call trader" << endl;

				// ============start trader thread============
				TraderCTP trader = TraderCTP(false, ms_locator_ptr, "138007", config_string);
				trader.run();
			}

			void StartUp::load_market() {
				cout << "call market" << endl;

				// ============start market thread============
				std::cout << "on load config_char:" << config_char << endl;
				std::cout << "on load config_string:" << config_string << endl;

				MarketDataCTP trader = MarketDataCTP(false, ms_locator_ptr, config_string);
				trader.run();
			}
		}
	}
}